//
//  YeeHomeVC.m
//  YeeShopProject
//
//  Created by CoderYee on 2017/2/27.
//  Copyright © 2017年 CoderYee. All rights reserved.
//

#import "YeeHomeVC.h"
#import "YeeKindVC.h"
#import "XRPageMainSecTableCell.h"
#import "YeeHomeOnlyImageTableViewCell.h"
#import "XRPageMainVideoMuiTableCell.h"
#import "YeeHomeCollectionReusableView.h"
#import "YeeHomeCommodityTableViewCell.h"
#import "YeeHomeCommodityTableViewCell.h"
#import "BAFPSLabel.h"
#import "TestVC.h"  //面试测试
@interface YeeHomeVC ()<SDCycleScrollViewDelegate,XRPageMainSecTableCelldelegate>
{
    CGFloat     _alphaMemory;
    UIButton    *_topButton;
}
@property (nonatomic, assign) BOOL statusBarStyleControl;

@end

@implementation YeeHomeVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)])
    {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [BAFPSLabel showInWindow:[UIApplication sharedApplication].keyWindow];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:_alphaMemory];
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
    if (_alphaMemory  == 0)
    {
        self.navigationController.navigationBar.tintColor = kNavBarThemeColor;
    }
    else
    {
        self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    }
    [self scrollViewDidScroll:self.collectionView];
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    if (_alphaMemory < 1) {
        [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:1];
    }
}

-(void)addNavbarView
{
    [super addNavbarView];
    UIButton *rightButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setFrame:CGRectMake(0, 0, 25, 25)];
    [rightButton setImage:[UIImage imageNamed:@"message_no_content"] forState:UIControlStateNormal];
    [rightButton setImage:[UIImage imageNamed:@"message_no_content"] forState:UIControlStateHighlighted];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
}
-(void)addOwnViews
{
    [super addOwnViews];

}
-(void)configOwnViews
{
    [super configOwnViews];
    self.collectionView.frame=CGRectMake(0, -64, kMainScreenWidth, kMainScreenHeight-49);
//    [self tablePullupHeaderRefresh:^{
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [self tableViewEndRefresh];
//        });
//    }];
//    [self tablePullDownFootRefresh:^{
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [self tableViewEndRefresh];
//        });
//    }];
}
#pragma mark --轮播图
-(SDCycleScrollView *)bannerheaderView
{
    SDCycleScrollView  *bannerHeaderView=[SDCycleScrollView  cycleScrollViewWithFrame:CGRectMake(0, 0, kMainScreenWidth,kMainBannerHight) delegate:self placeholderImage:[UIImage imageNamed:@"activity_default"]];
    bannerHeaderView.pageControlStyle=SDCycleScrollViewPageContolStyleClassic;
    bannerHeaderView.bannerImageViewContentMode=UIViewContentModeScaleAspectFill;
    bannerHeaderView.pageDotColor=kWhiteColor;
    [bannerHeaderView setBackgroundColor:kWhiteColor];
    bannerHeaderView.pageDotImage=[UIImage imageNamed:@"HX_Banner_dot_nor"];
    bannerHeaderView.currentPageDotImage=[UIImage imageNamed:@"HX_Banner_dot_current"];
    bannerHeaderView.currentPageDotColor=kNavBarThemeColor;
    bannerHeaderView.imageURLStringsGroup=@[@"http://upload-images.jianshu.io/upload_images/3356997-1f957c03fd1d4614.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240",@"http://upload-images.jianshu.io/upload_images/3534836-ceb716b394ae8a63.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240",@"http://upload-images.jianshu.io/upload_images/3408986-6b247074e37310a5.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"];
   // self.collectionView.coll=bannerHeaderView;
    
    return bannerHeaderView;
}
-(void)collectionViewRegisterCollectionCell
{
     [super collectionViewRegisterCollectionCell];
     [self.collectionView registerClass:[XRPageMainSecTableCell class] forCellWithReuseIdentifier:NSStringFromClass([XRPageMainSecTableCell class])];
     [self.collectionView registerClass:[YeeHomeOnlyImageTableViewCell class] forCellWithReuseIdentifier:NSStringFromClass([YeeHomeOnlyImageTableViewCell class])];
    [self.collectionView registerClass:[XRPageMainVideoMuiTableCell class] forCellWithReuseIdentifier:NSStringFromClass([XRPageMainVideoMuiTableCell class])];
    [self.collectionView registerClass:[YeeHomeCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([YeeHomeCollectionReusableView class])];
    
    [self.collectionView registerClass:[YeeHomeCommodityTableViewCell class] forCellWithReuseIdentifier:NSStringFromClass([YeeHomeCommodityTableViewCell class])];
    //
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 6;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    if (section==0||section==1||section==2||section==3||section==4)
    {
        return 1;
        
    }else
    {
        return   4;
    }
    
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==0)
    {
        XRPageMainSecTableCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XRPageMainSecTableCell class]) forIndexPath:indexPath];
    cell.imageArray=@[@"icon_shouye_3",@"icon_shouye_4",@"icon_shouye_1",@"icon_shouye_2",@"icon_shouye_2",@"icon_shouye_3",@"icon_shouye_4",@"icon_shouye_1",];
        cell.itemArray=@[@"每日任务",@"商城",@"直播",@"游戏",@"超市",@"水果",@"美食",@"服饰"];
        return cell;
    }else if (indexPath.section==1)
    {
        YeeHomeOnlyImageTableViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YeeHomeOnlyImageTableViewCell class]) forIndexPath:indexPath];
        return cell;
        
    }else if (indexPath.section==2)
    {
        XRPageMainVideoMuiTableCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XRPageMainVideoMuiTableCell class]) forIndexPath:indexPath];
        
        return cell;
        
    }else if (indexPath.section==3)
    {
        YeeHomeCommodityTableViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YeeHomeCommodityTableViewCell class]) forIndexPath:indexPath];
        return cell;

    }
    YeeHomeOnlyImageTableViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YeeHomeOnlyImageTableViewCell class]) forIndexPath:indexPath];
    return cell;
    
   

    
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView  *headerView=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class]) forIndexPath:indexPath];
    if (indexPath.section==0)
    {
        [headerView addSubview:[self bannerheaderView]];
         return headerView;
    } else if (indexPath.section==1)
    {
       return headerView;
        
    }
    else
    {
       YeeHomeCollectionReusableView *headerView=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([YeeHomeCollectionReusableView class]) forIndexPath:indexPath];
         return headerView;
    }
    return headerView;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    
    if (section==0)
    { return CGSizeMake(kMainScreenWidth, kMainBannerHight);
    }else if (section==1)
    { return CGSizeMake(kMainScreenWidth, 0.01);
    }else
    { return CGSizeMake(kMainScreenWidth,30);
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        return CGSizeMake(kMainScreenWidth, 140);
  
    }else if (indexPath.section==1)
    {
      return CGSizeMake(kMainScreenWidth, kMainBannerHight);
        
    }else if (indexPath.section==2)
    {
      return CGSizeMake(kMainScreenWidth, 220);
    }else if (indexPath.section==3)
    {
     return CGSizeMake(kMainBannerHight-5,kMainBannerHight-15);

    }else if (indexPath.section==4)
    {
        return CGSizeMake(kMainScreenWidth, kMainBannerHight);
    }
    return CGSizeMake(kMainScreenWidth, 220);
    
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
{
    return 0;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    TestVC *vc=[[TestVC alloc] init];
    vc.hidesBottomBarWhenPushed=YES;

    [self.navigationController pushViewController:vc animated:YES];
    
}
#pragma mark 监听状态
//
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat offsetY = scrollView.contentOffset.y;
    //DLog(@"%f",offsetY);
    if (offsetY > -64 && offsetY <= 64)
    {
        _statusBarStyleControl = YES;
        if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
            [self setNeedsStatusBarAppearanceUpdate];
        }
        self.navigationController.navigationBar.tintColor = [UIColor blackColor];
        _alphaMemory = offsetY/64 >= 1 ? 1 : offsetY/64;
        [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:_alphaMemory];
    }
    else if (offsetY <= -64)
    {
        _statusBarStyleControl = NO;

        if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
            [self setNeedsStatusBarAppearanceUpdate];
        }
        self.navigationController.navigationBar.tintColor = kNavBarThemeColor;
        _alphaMemory = 0;
        [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:0];
    }
    else if (offsetY > 64)
    {
        _alphaMemory = 1;
        [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:1];
    }
    
}
#pragma mark --点击单元格
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.navigationController pushViewController:[YeeKindVC new] animated:YES];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#ifdef __IPHONE_7_0
- (UIStatusBarStyle)preferredStatusBarStyle
{
    if (_statusBarStyleControl) {
        return UIStatusBarStyleDefault;
    }
    else {
        return UIStatusBarStyleLightContent;
    }
}
- (BOOL)prefersStatusBarHidden
{
    return NO;
}
#endif
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
